package com.app.micaihu.utils;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.App;
import com.app.micaihu.bean.AppVersion;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.components.ParamMap;
import com.app.micaihu.listener.HttpListener;
import com.app.utils.ChannelUtil;
import com.app.utils.util.AppTools;
import com.app.utils.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

/**
 * 检查版本更新
 * Created by linkai on 2016/8/14.
 */
public class CheckVersionUtils {


    /**
     * 检测版本更新
     *
     * @param mContext 上下文对象
     * @param isShow 是否展示对话框
     */
    public void checkVertion(final Context mContext, final boolean isShow) {
        //设置友盟分享dialog
        final ProgressDialog dialog =  new ProgressDialog(mContext);
        if(isShow){
            dialog.setMessage("正在检测最新版本...");
            dialog.show();
        }
        String channel = ChannelUtil.getChannel(AppApplication.getInstance(), "test");
        //请求更新接口
        ParamMap paramMap = new ParamMap();
        paramMap.putEncode("type","1");
//        paramMap.putEncode("versionNum", AppTools.getVersionName());
        paramMap.putEncode("channel", channel);
        paramMap.putEncode("versioncode", AppTools.getVersionCode()+"");
        HttpUtils.ParmsMapMD5(paramMap);
        HttpListener<DataBean<AppVersion>> httpListener = new HttpListener<DataBean<AppVersion>>() {
            @Override
            public void onError(VolleyError error) {
                if(dialog != null && dialog.isShowing()){
                    try{
                        dialog.cancel();
                    }catch (Exception e){
                    }
                }
                if(isShow){
                    ToastUtils.makeText("服务器连接失败,请稍后再试");
                }
            }

            @Override
            public void onSuccess(DataBean<AppVersion> response) {
                if(dialog != null && dialog.isShowing()){
                    try{
                        dialog.cancel();
                    }catch (Exception e){
                    }
                }
                if(response.noError()){
                    setVersionResult(response.getData(),isShow,mContext);
                }

            }
        };
        HttpUtils.addPostRequest(URIContant.VERSION, new TypeToken<DataBean<AppVersion>>() {
        }.getType(), "", paramMap, httpListener);

    }


    /**
     * 设置版本结果
     * @param appVersion
     */
    private void setVersionResult(final AppVersion appVersion,boolean isshow, final Context context){
        if(appVersion == null) {
            ToastUtils.showText("更新失败,请稍候再试");
            return;
        }
        final Dialog alertDialog =  new Dialog(context, R.style.CustomDialogStyle);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_check_upgrade, null);
        if("2".equals(appVersion.getStatus())){//强制更新

            ((TextView)view.findViewById(android.R.id.text1)).setText("报告长官, 发现新版本 " + appVersion.getVarsionNum());
            ((TextView)view.findViewById(android.R.id.text2)).setText(appVersion.getUpdateLog());
            view.findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    App app = new App(context);
                    app.setApp_url(appVersion.getUrl());
                    app.startDownApp();
                }
            });
            view.findViewById(android.R.id.button2).setVisibility(View.INVISIBLE);
            alertDialog.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            alertDialog.setContentView(view);
            alertDialog.setCancelable(false);
            alertDialog.setCanceledOnTouchOutside(false);

        }else if("1".equals(appVersion.getStatus())){//建议更新
            ((TextView)view.findViewById(android.R.id.text1)).setText("报告长官, 发现新版本 " + appVersion.getVarsionNum());
            ((TextView)view.findViewById(android.R.id.text2)).setText(appVersion.getUpdateLog());
            view.findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(alertDialog != null){
                        alertDialog.dismiss();
                    }
                    App app = new App(context);
                    app.setApp_url(appVersion.getUrl());
                    app.startDownApp();
                }
            });
            view.findViewById(android.R.id.button2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(alertDialog != null){
                        alertDialog.dismiss();
                    }
                }
            });
            alertDialog.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            alertDialog.setContentView(view);
            alertDialog.setCanceledOnTouchOutside(true);
            alertDialog.setCancelable(true);
        } else {
            if(!isshow){
                return;
            }
            ((TextView)view.findViewById(android.R.id.text1)).setText("报告长官, 当前已经是最新版本 ");
            TextView submitView = (TextView) view.findViewById(android.R.id.button1);
            submitView.setText("确定");
            submitView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (alertDialog != null) {
                        alertDialog.dismiss();
                    }
                }
            });
            view.findViewById(android.R.id.button2).setVisibility(View.INVISIBLE);
            alertDialog.requestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY);
            alertDialog.setContentView(view);
            alertDialog.setCanceledOnTouchOutside(true);
            alertDialog.setCancelable(true);
        }
        try {
            if(!((Activity)context).isFinishing() ){
                alertDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}




